import * as Cesium from 'cesium'

export default class BillboardPrimitiveManage {
  constructor (options) {
    this.billboardCollection = new Cesium.BillboardCollection({
      blendOption:Cesium.BlendOption.OPAQUE,
      ...options
    })
    this._collection = new Cesium.AssociativeArray()
  }

  add(billboardParams){
    const {id} = billboardParams;
    const renderId =Cesium.defined(id) ? id : Cesium.createGuid();

    const billboard = this.billboardCollection.add({
      ...billboardParams
    })

    billboard.id = billboard;
    billboard.renderId = renderId

    this._collection.set(renderId,billboard)
    return billboard;
  }

  contains(billboardPrimitive){
    return this.billboardCollection.contains(billboardPrimitive)
  }

  destroy(){
    this._collection.removeAll()
    this.billboardCollection.destroy()
  }

  get(index){
    return this.billboardCollection.get(index)
  }

  getById(key){
    return this._collection.get(key)
  }

  isDestroyed(){
    return this.billboardCollection.isDestroyed()
  }

  remove(billboardPrimitive){
    const findIndex = this._collection.values.findIndex(val => val === billboardPrimitive);

    if(findIndex === -1){
      console.warn(`PointPrimitiveManager.js Collection not find ：`,billboardPrimitive);
    }else{
      const resultId = this._collection.values[findIndex].renderId;
      this._collection.remove(resultId)
    }
    return this.billboardCollection.remove(billboardPrimitive)
  }

  removeById(key){
    const point = this._collection.get(key)
    return  this.remove(point)
  }

  removeAll(){
    this._collection.removeAll()
    this.billboardCollection.removeAll();
  }

  update(frame){
    if(this.isDestroyed()){
      return false
    }
    this.billboardCollection.update(frame)
  }

}